package IO流;

import java.io.IOException;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousServerSocketChannel;
import java.nio.channels.AsynchronousSocketChannel;
import java.nio.channels.CompletionHandler;

public class Aio_Ser_Cli {
    //模拟服务端
    public static void main(String[] args) throws IOException, InterruptedException {
        AsynchronousServerSocketChannel open = AsynchronousServerSocketChannel.open();
        open.bind(new InetSocketAddress(9999));
        open.accept(null, new CompletionHandler<AsynchronousSocketChannel, Object>() {
            @Override
            public void completed(AsynchronousSocketChannel result, Object attachment) {
                System.out.println("服务端连接客户端连接成功！");
                result.write(ByteBuffer.wrap("客户端你好".getBytes()), null, new CompletionHandler<Integer, Object>() {
                    @Override
                    public void completed(Integer result, Object attachment) {
                        System.out.println("服务端发送客户端信息长度:"+result);
                    }

                    @Override
                    public void failed(Throwable exc, Object attachment) {
                        System.out.println("服务端发送信息失败！");
                    }
                });
            }

            @Override
            public void failed(Throwable exc, Object attachment) {
                System.out.println("服务端连接客户端失败！");
            }
        });
        while (true){
            System.out.println("阿巴阿巴");
            Thread.sleep(1000);
        }
    }
}
class Cli{//模拟客户端
    public static void main(String[] args) throws IOException, InterruptedException {
        AsynchronousSocketChannel open = AsynchronousSocketChannel.open();
        open.connect(new InetSocketAddress("127.0.0.1", 9999), null, new CompletionHandler<Void, Object>() {

            @Override
            public void completed(Void result, Object attachment) {
                System.out.println("客户端连接服务端成功！");
                ByteBuffer buffer = ByteBuffer.allocate(1024);
                open.read(buffer, null, new CompletionHandler<Integer, Object>() {
                    @Override
                    public void completed(Integer result, Object attachment) {
                        System.out.println("服务端发送过来的信息为:"+new String(buffer.array(),0,result));
                        System.out.println("客户端读取服务端信息成功！");
                    }

                    @Override
                    public void failed(Throwable exc, Object attachment) {
                        System.out.println("客户端读取服务端信息失败！");
                    }
                });
            }

            @Override
            public void failed(Throwable exc, Object attachment) {
                System.out.println("客户端连接服务端失败！");
            }
        });
        while (true){
            System.out.println("嘎啦嘎啦");
            Thread.sleep(1000);
        }
    }
}
